import 'dart:io';

import 'package:flutter/material.dart';
import 'package:gold/base/provider_scaffold.dart';
import 'package:gold/http/api_gold.dart';
import 'package:gold/model/gold_recover_shop_model.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/ui/home/recover/shop_list_viewmodel.dart';
import 'package:gold/utils/geocode/geocode_entity.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

/// @description: 门店列表
/// @author tangzhentao
/// @created at 20:33 2024/12/10


class ShopListPage extends StatelessWidget {
  const ShopListPage({super.key});

  @override
  Widget build(BuildContext context) {
    return ProviderScaffold<ShopListViewmodel>(
        appBar: (_) => xfgAppbar(title: '门店列表'),
        resizeToAvoidBottomInset: false,
        onInit: (data) => data.getLocation(),
        model: ShopListViewmodel(ApiGold.recycleStore),
        toplessBuilder: (ctx, shop) => Column(
          children: [
            /// 当前位置
            GestureDetector(
              onTap: shop.getLocation,
              child: Container(
                  margin: EdgeInsets.only(bottom: XfgSize().size10),
                  padding: EdgeInsets.symmetric(horizontal: XfgSize().size12),
                  color: XfgColor.goldRecoverTips,
                  alignment: Alignment.center,
                  height: XfgSize().size32,
                  child: Row(
                    children: [
                      Expanded(child: Builder(builder: (con) {
                        final location = con.select<ShopListViewmodel, GeocodeEntity?>((k) => k.location);
                        return XfgText('当前为准：${location?.city??''}', fontSize: XfgSize().text14, color: XfgColor.red);
                      })),
                      Image.asset(Res.imagesShopListLocation, width: XfgSize().size14, height: XfgSize().size17),
                      XfgText('  重新地位', fontSize: XfgSize().text14, color: XfgColor.red)
                    ],
                  )
              ),
            ),
            /// 搜索
            Container(
              margin: EdgeInsets.only(bottom: XfgSize().size5),
              padding: EdgeInsets.symmetric(horizontal: XfgSize().size10),
              child: Row(
                children: [
                  SizedBox(width: XfgSize().size102, child: XfgText('江苏省', fontSize: XfgSize().text20, color: XfgColor.text1D, bold: FontWeight.bold)),
                  Expanded(child: Container(
                    height: XfgSize().size38,
                    padding: EdgeInsets.only(left: XfgSize().size18, right: XfgSize().size15),
                    decoration: BoxDecoration(color: XfgColor.white, borderRadius: BorderRadius.circular(XfgSize().size38)),
                    child: Row(
                      children: [
                        Expanded(child: TextField(
                          controller: shop.searchController,
                          focusNode: shop.searchNode,
                          style: TextStyle(fontSize: XfgSize().text16, color: XfgColor.text1),
                          decoration: InputDecoration(
                            contentPadding: EdgeInsets.zero,
                            isCollapsed: true,
                            border: InputBorder.none,
                            hintText: '请输入门店名称',
                            hintStyle: TextStyle(fontSize: XfgSize().text16, color: XfgColor.text3),
                          ),
                        )),
                        GestureDetector(
                          onTap: shop.firstLoad,
                          child: Image.asset(Res.imagesIntegralSearch, width: XfgSize().size20 - XfgSize().size0_5, height: XfgSize().size21, color: XfgColor.colorB8),
                        )
                      ],
                    ),
                  ))
                ],
              ),
            )
          ],
        ),
        builder: (ctx, shop) => SmartRefresher(
          controller: shop.controller,
          enablePullDown: true,
          enablePullUp: true,
          onRefresh: () => shop.refresh(),
          onLoading: () => shop.loadMore(),
          child: ListView.builder(
            itemCount: shop.list.length,
            itemBuilder: (_, index) {
              final model = shop.list[index];
              final isLast = shop.list.length == index + 1;
              return Container(
                margin: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size10, XfgSize().size10, isLast ? XfgSize().bottomBarHeight:0),
                padding: EdgeInsets.fromLTRB(XfgSize().size12, XfgSize().size17, XfgSize().size14, XfgSize().size14),
                decoration: white6Decoration,
                child: Column(
                  children: [
                    Row(
                      children: [
                        Expanded(child: XfgText('${model.name?? ''}\n', fontSize: XfgSize().text18, color: XfgColor.text1D, bold: FontWeight.bold, height: 2, children: [
                          TextSpan(text: _getAddress(model), style: TextStyle(fontSize: XfgSize().text16, color: XfgColor.text1))
                        ])),
                        GestureDetector(
                          onTap: () => XfgNav.pop(model),
                          child: Container(
                            width: XfgSize().size62, height: XfgSize().size32,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(color: XfgColor.shopListReserveBg, borderRadius: BorderRadius.circular(XfgSize().size5)),
                            child: XfgText('预约', fontSize: XfgSize().text14, color: XfgColor.shopListReserve, height: Platform.isIOS ? 1:null),
                          ),
                        )
                      ],
                    ),
                    SizedBox(height: XfgSize().size8),
                    Row(
                      children: [
                        Expanded(flex: 3,child: XfgText('营业时间：${_getTime(model.workingTime, model.closingTime)}', fontSize: XfgSize().text14, color: XfgColor.text3)),
                        Expanded(flex: 2,child: XfgText('联系人： ${model.contactName??''}', fontSize: XfgSize().text14, color: XfgColor.text3)),
                      ],
                    )
                  ],
                ),
              );
            },
          ),
        )
    );
  }

  /// 获取展示地址
  String _getAddress(GoldRecoverShopModel model) {
    String address = '';
    if (model.province?.isNotEmpty == true) {
      address += model.province!;
    }
    if (model.city?.isNotEmpty == true) {
      address = address + (address.isNotEmpty ? '-':'') + model.city!;
    }
    if (model.area?.isNotEmpty == true) {
      address = address + (address.isNotEmpty ? '-':'') + model.area!;
    }
    if (model.address?.isNotEmpty == true) {
      address = address + (address.isNotEmpty ? '-':'') + model.address!;
    }
    return address;
  }

  /// 获取营业时间
  String _getTime(String? start, String? end) {
    if (start?.isNotEmpty == true && end?.isNotEmpty == true) {
      return '$start-$end';
    }
    return '--';
  }
}